package com.yueshop.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.yueshop.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000117622824";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQChN0aAknDZvT1e1T3Z8QyI8sFH4yqyiH/YxpHtqp9ccCo9GpYsapoT2IZSqzht1dY2GPCLMuNQVO9JNzDB72Yf5CKJGq0i9pJiaJPWyt1YY7zbj04nC9o4PDyhJL/O1O1FONmBv38ChhCG1spr89q7Ie7pEH6aEZmD95mlowjYk0yuU5iIeRqk728jG1YRJhV/g7bL9EPZlCpt9tXzRIj3vVn2Akxm0x7X1IWx2dzV3LG4Dpg5eCVo3Bs4YlQQK8gKl8A+sZtJ/TB41AwaDadU0RGC4PSVbwEQogx3mb2eo/DuLnGoV9LoDUTQQZO8+9qeGOX0WS3Ppao5rmo1h5ZrAgMBAAECggEARRWezLH0UhpaFi1jF+XH7Ve5o8ihH2iHAKn3FtWq/L3EG/Msr1PvOtnqlbpxr/+WkBAX5E6mQmJjOAjCiBjCtp/YfNBFqVnoWOtfjwfX2mzDHYNHhkv582c8xAEvgL6V/rfh3UvmbXpt0ppwm9ofvVlJO+6pkfRAyOBtoyE+RTAK5/WR1p1H1QQLgii9tqDzB+zyNoVuWAUw43/hxmJXiCd9tnsPXeTiP7IAYLlOw/AN3Bdzu8R84sWGq4HEI5s3gf9ERFfPBOcl8+lo/UBfGZlNWrcCWeMX8bkgzwV61EvqxdXr9GvdTXtRr+LDUWRFoZZjXZwvCRIhtMypInmbAQKBgQDiRHIW+v4r2fvM27LboKdP3ZK/XQQW8t9xLF0qdOunjGtwo654nRpG3M7c9r8QCMYq8yKu6hvn89tF6b8XiRhzISa/k1vy8mgKWD5LoDY8b++UlRUNpFx7D1t8XGMX3WbJv0n+c3Sv7i+MVwCCfWlArQyTtni30UZQZdHh3cuxSwKBgQC2ZoWkTJ9MsrSfajDNSXTgK0Ht4plZ9H4rDEkmbELNtPtxYUUUbS3ZXF+cPGuRbfgjHfyA0SBvtV+MaP7nvapBk3BKCNKVFWuzt8yCd1mCy5KOpjfbRAKlUYTRJzme2OoHMKLf/OtvK2A5z8+a0XYGJNxKxFpY/rCizEF6dWObYQKBgDrZHGc3rmWPUzsLpPAUT4+pDCKzOX4SeuS/ahfBwwyXq9Xm7Fi4W340raSwlC7CaXAAPnQJtWpVdSEw0x9266H97EsFM3PzcfXGz6TxrUl4v3IAeDJNHjRvYRbuHGO3lKVCGCeStbWQGFPCyQInRfmpSE0etzTvu/7rDHRXMEQTAoGAck4BRhs2YSxEgPpdJuj3s1uob1ln3LWZM4T5enixzJO0VzMrxUc+Sk7XI+PBTVvq+qbaDCgB+b9h524NAFEdlwa8gcjRntfTrMZsBP6iO5Mzfu2vZ1tRhSR25VN9ioxI7MSP7WpkaEB4hwIyCcVl/yeuXbR7IfZJ31u9VO6nw0ECgYAgGy2fdV5xNkhePym8Bwac3j71KMHirUHTpxxW5nx4rjQ8FYc0FHR/9UsJuaDx48fBQtKIEandGj0qRFkk2ML0Y0jR2YKcgJPOPedLValhOgXqaIf+ICezHqQfso7JIKl+gXz3Hea2nPJP7XBQRQAkGdryEZiLdOu9v1gowK2p3Q==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAugn1LnHlqjx+pqnaV8US3x22gZYyIDBIVcG2QlrtDSkSl/NInrp6SKp9X697CJzNCQ/HJ0hSKXz6ed53YF8Pdw7qkW46zuD/A9xqcwElH0jD+0jHvlaJ10h9P1OVuO1PLt2Bl73EPy4X6RvXs/cPFDNx6SwfdH+hYY9c6hcC9WIgMbMS6p3B40inhin86ORPJzWAglJ92Q56mtZ/Xrt7qT3D4piRONeE/uAjulBnqgQLvwOHEYWhv2yIZ9lDWQ+Q+VsE63X4eM7eOtoB80s/CBYUnTLcnhmXkR34N2h6/dMu/FF6VKcxQTiFPuONGJoT9sBt4fY9tG4y1cu750PIzQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://1oqmejwe6k.xuduan.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://order.yueshop.com/orderList.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private  String timeout_express = "1m";
    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
